package servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import anuncio.Anuncio;
import anuncio.ControlAnuncio;

import categoria.Categoria;

/**
 * Servlet implementation class ServletAlteracoesAnuncio
 */
public class ServletAlteracoesAnuncio extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletAlteracoesAnuncio() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Anuncio anun = new Anuncio();
		ControlAnuncio ctr = new ControlAnuncio();
		String mensagem = "";
		String dta = request.getParameter("data");
		SimpleDateFormat mask = new SimpleDateFormat("dd/MM/yyyy");
		Date datanasc;
		try {
			datanasc = (Date) mask.parse(dta);
			anun.setData_anuncio(datanasc);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		try {
		anun.setCidade(request.getParameter("cit"));
		anun.setDesc_anuncio(request.getParameter("desc"));
		anun.setEstado(request.getParameter("est"));
		anun.setId_usuario(1);
		anun.setTitulo(request.getParameter("tit"));
		anun.setValoranuncio(Double.parseDouble(request.getParameter("val")));
		anun.setId_anuncio(Integer.parseInt(request.getParameter("id")));
		Categoria cat = ctr.categoriaespecifica(request.getParameter("categoria"));
		anun.setCat(cat);
		
		
			ctr.alterando(anun);
			mensagem = "alterado";
		} catch (Exception e) {
			mensagem = "erro";
		}
		
		
		response.sendRedirect("mensagem.jsp?msg=" + mensagem);
		
		
	}

}
